package cn.com.blackboard.DataModule4B2.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cm.com.blackboard.DataModule4B2.customizeConfigUtils.BatchImportUtil;
import cn.com.blackboard.DataModule4B2.BBDataUtils.CourseUtil;
import cn.com.blackboard.DataModule4B2.dao.CourseInfoMapper;
import cn.com.blackboard.DataModule4B2.model.CourseInfo;
import cn.com.blackboard.DataModule4B2.service.CourseService;
import cn.com.blackboard.DataModule4B2.util.BatchResultModel;
import cn.com.blackboard.DataModule4B2.util.BatchResultModel.Status;
import cn.com.blackboard.DataModule4B2.util.Condition;

@Service("CourseService")
public class CourseServiceImpl implements CourseService{

	private static final Logger logger = LoggerFactory.getLogger(CourseServiceImpl.class);
	
	@Autowired
	private CourseInfoMapper courseInfoMapper;
	
	public int insert(CourseInfo courseInfo) {
		// TODO Auto-generated method stub
		return courseInfoMapper.insert(courseInfo);
	}

	@Override
	public List<CourseInfo> getCourse() {
		return getCourse("");
	}

	@Override
	public int updateStatus(String courseId) {
		// TODO Auto-generated method stub
		return courseInfoMapper.updateRecordImportedByCourseId(courseId);
	}
	
	@Override
	public List<CourseInfo> getCourse(String jsonCondition) {
		Condition condition = Condition.ParseCondition(jsonCondition);
		String conditionStr = Condition.RenderConditoin(condition);
		List<CourseInfo> courseInfoList = null;
		/*if(HierarchicalQueryLoader.isHierarchicalQuery(CourseInfo.class)){
			MyBatisXMLModel mbxModel = HierarchicalQueryLoader.getMyBatisXMLModelByClsName(CourseInfo.class);
			conditionStr = Condition.RenderConditoin(condition, mbxModel.getMainTable());
			return courseInfoList = courseInfoMapper.selectByHierarchicalCondition(mbxModel.getFixedColList(), mbxModel.getMainTable(), 
					mbxModel.getSubTable(), mbxModel.getOnCondition(), conditionStr);		
		}*/
		courseInfoList = courseInfoMapper.selectByCondition(conditionStr);
		
		return courseInfoList;
	}

	@Override
	public List<BatchResultModel> importCourse(List<CourseInfo> courseInfoLst) {
		logger.info("start to import course");
		int count = 0;
		List<BatchResultModel> list = new ArrayList<BatchResultModel>();
		for(CourseInfo ci:courseInfoLst){
			BatchResultModel batchResultModel = new BatchResultModel();
			batchResultModel.setModelClsName(CourseInfo.class.getName());
			batchResultModel.setModelClsAttr("courseId-courseName");
			batchResultModel.setValue(ci.getCourseId()+"-"+ci.getCourseName());
			Status status = CourseUtil.importCourse(ci);
			count += status.isSuccess()?1:0;
			if(status.isSuccess())
				updateStatus(ci.getCourseId());
			list.add(batchResultModel);
		}
		logger.info("Course need to be imported is "+courseInfoLst.size()+" the num imported indeed is "+count);
    	return list;
	}

	@Override
	public List<BatchResultModel> batchImportCourse() {
		String conditionStr = BatchImportUtil.getBatchFilterCondition(CourseInfo.class.getName());
		List<CourseInfo> courseInfoLst = courseInfoMapper.selectByCondition(conditionStr);
		List<BatchResultModel> list = importCourse(courseInfoLst);
		return list;
	}
}
